using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.Triggers
{
    [Serializable]
    public class ManualTriggerOutput:ResultBase
    {
        private string _User;

        public string User
        {
            get { return _User; }
            set { _User = value; }
        }
	

        public ManualTriggerOutput():base(Guid.Empty,Guid.Empty)
        {
            this._User = Environment.UserName;
        }

        public ManualTriggerOutput(Guid componentID, Guid jobTicketID, string user) : base(componentID, jobTicketID)
        {
            this._User = user;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Trigger");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "User", this.User);
            return xNode;
        }

        public override ResultBase Instantiate(XmlNode xNode)
        {
            ManualTriggerOutput output = new ManualTriggerOutput();
            string typeName = xNode.Attributes.GetNamedItem("Type").Value;
            if (typeName == this.GetType().FullName)
            {
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.User = xNode.Attributes.GetNamedItem("User").Value;

                return output;
            }
            else
            {
                throw new Exception("Invalid type to deserialize from xml doc");
            }
        }
    }
}
